# == Schema Information
#
# Table name: radcheck
#
#  id                :integer          not null, primary key
#  username          :string(64)       default(""), not null
#  attr              :string(64)       default(""), not null
#  op                :string(2)        default("=="), not null
#  value             :string(253)      default(""), not null
#  radcheckable_id   :integer
#  radcheckable_type :string(255)
#

class Radcheck < ActiveRecord::Base
  self.table_name = "radcheck"
  #belongs_to :user#, :primary_key => :username, :foreign_key => :username
  belongs_to :radcheckable, :polymorphic => true

  validates_presence_of :username, :attr, :op, :value, :radcheckable_type, :radcheckable_id
  validates_uniqueness_of :attr, :scope => [:radcheckable_type, :radcheckable_id]

  after_update :update_user_password

  protected 

  def update_user_password
  	if self.attr == Settings.check_types['password']
        # update_column don't callback
      self.radcheckable.update_column(:password, self.value)
  	end
  end
end
